package cn.wk.libs4a.view.phonecontact;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wk.libs4a.R;
import cn.wk.libs4a.WKApplication;

public class WKPhoneContactAdapter extends BaseAdapter {

	private Context context;
	private List<WKPhoneContactBean> contacts;// 需要显示的联系人,已排序

	public WKPhoneContactAdapter(Context context) {
		super();
		this.context = context;
	}

	/**
	 * 获取全局APP
	 */
	public WKApplication app() {
		return (WKApplication) context.getApplicationContext();
	}

	public void setContacts(List<WKPhoneContactBean> contacts) {
		this.contacts = contacts;
	}

	public List<WKPhoneContactBean> getContacts() {
		return contacts;
	}

	@Override
	public int getCount() {
		return contacts == null ? 0 : contacts.size();
	}

	@Override
	public Object getItem(int position) {
		return contacts == null ? null : contacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Hodler hodler = null;
		if (convertView == null) {
			hodler = new Hodler();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.wk_act_phone_contact_list_item, null);
			hodler.tv_letter = (TextView) convertView
					.findViewById(R.id.tv_letter);
			hodler.tv_letter.setTextSize(26);
			hodler.contact = (TextView) convertView.findViewById(R.id.contact);
			hodler.phone = (ImageView) convertView.findViewById(R.id.phone);
			hodler.phoneNumber = (TextView) convertView
					.findViewById(R.id.phoneNumber);
			convertView.setTag(hodler);
		} else {
			hodler = (Hodler) convertView.getTag();
		}

		if (position == 0
				|| !getFirstLetter(contacts.get(position - 1).getName())
						.equals(getFirstLetter(contacts.get(position).getName()))) {// 如果是第一个或者首字母和上一个首字母不同
			hodler.tv_letter.setVisibility(View.VISIBLE);
			hodler.tv_letter.setText(WKPhoneContactUtils
					.getPingYin(contacts.get(position).getName()).toUpperCase()
					.substring(0, 1));
		} else {
			hodler.tv_letter.setVisibility(View.GONE);
		}

		hodler.contact.setText(contacts.get(position).getName());
		hodler.phoneNumber.setText(contacts.get(position).getPhoneNo());

		if (WKPhoneContactUtils.showAvatar) {
			if (contacts.get(position).isHasAvatar()) {
				app().imageLoader.displayImage(contacts.get(position)
						.getImgKey(), hodler.phone);
			} else {
				hodler.phone.setImageResource(R.drawable.ico_user_128);
			}
		}
		return convertView;
	}

	private class Hodler {
		TextView tv_letter, contact;
		ImageView phone;
		TextView phoneNumber;
	}

	private String getFirstLetter(String s) {
		if (null == s) {
			return null;
		}
		return WKPhoneContactUtils.getPingYin(s).substring(0, 1).toUpperCase();
	}

	/**
	 * 计算指定字母索引对应contacts中位置
	 * 
	 * @param index
	 * @return
	 */
	public int getSectionForPosition(char index) {
		if (contacts != null) {
			for (int i = 0; i < contacts.size(); i++) {
				String pinyin = WKPhoneContactUtils.getPingYin(contacts.get(i)
						.getName());
				if (TextUtils.isEmpty(pinyin)) {
					continue;
				}
				if (pinyin.toUpperCase().charAt(0) == index) {
					return i;
				}
			}
		}
		return -1;
	}

}
